#include  <stdint.h>
#include  "mid_lcd.h"
#include  "mid_key.h"
#include  "mid_beep.h"
#include  "app.h"



static uint8 LinkTypetempH, LinkTypetempM, LinkTypetempL;
/*********************************************************************************************************
** Function name:
**
** Descriptions:	
**
**-------------------------------------------------------------------------------------------------------
** Modified by: 
** Modified date : 
**-------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static void disp_ui_v(uint8 dis1, uint8 dis2, uint8 dis3)
{

		if(UIoperate.CodeFlag)
			mid_lcdbianma(DISPON);
		else
			mid_lcdbianma(DISPOFF);
		
		if(UIoperate.Wrong)
			mid_lcdcuowu(DISPON);
		else
			mid_lcdcuowu(DISPOFF);		

	  if(UIoperate.LockFlag)
			mid_lcdsuoding(DISPON);
		else
			mid_lcdsuoding(DISPOFF);			
		
	  if(UIoperate.ContinuesFlag)
			mid_lcdlianxu(DISPON);
		else
			mid_lcdlianxu(DISPOFF);


	  if(UIoperate.Correct)
			mid_lcdzhengque(DISPON);
		else
			mid_lcdzhengque(DISPOFF);		
		if(UIoperate.Data)
			mid_lcdshuju(DISPON);
		else
			mid_lcdshuju(DISPOFF);
		if(UIoperate.Addr)
			mid_lcddizhi(DISPON);
		else
			mid_lcddizhi(DISPOFF);
		
		if(UIoperate.RdFlag)
			mid_lcdduchu(DISPON);
		else
			mid_lcdduchu(DISPOFF);
		if(UIoperate.WrFlag)
			mid_lcdxieru(DISPON);
		else
			mid_lcdxieru(DISPOFF);
		
	  UIpos[0] = 15;//F
		UIpos[1] = UIoperate.NowSet/10;//
		UIpos[2] = UIoperate.NowSet%10;//		
	  mid_lcddisp_line1(UIpos[0], UIpos[1], UIpos[2]);
	  
	  mid_lcddisp_line2(dis1, dis2, dis3);
		mid_lcdp4(DISPON);
		strLcdD.frush = 1;
   

}
			 
			 
void UI_LinkType(void)
{

		cmdPROBUF tmp;
      if(GetKey(KEY_PRESS_UP))   
			{						
							BEEP_ON();
							UIoperate.Wrong = 0;
							UIoperate.Test = 0;
				
							switch (valuekey.val)
							 {
									case 0x21:										//READ     
											BEEP_ON();										
											UIoperate.RdFlag = 1;											
											UIoperate.WrFlag = 0;
											tmp.ch = 0;
											tmp.cmd = 0x3e;
											tmp.addr = 0;
											write_cmfbuf(&tmp);
											UIoperate.Correct = 0;
											UIoperate.Addr = 0;
											UIoperate.LastOperate = 0;
											disp_ui_v( ReadLinkType/100, ReadLinkType/10%10, ReadLinkType%10);	

									break;
									
									
									case 0x23:     								//0
									case 0x31:     								//7
									case 0x33:   									//8
									case 0x35:    								//9
									case 0x41:      							//4
									case 0x43:      							//5
									case 0x45:      							//6
									case 0x53:      							//2
									case 0x55:      							//3
									case 0x51:      							//1	
											
											UIoperate.Data = 0;
											UIoperate.Wrong = 0;
											UIoperate.Correct = 0;
											UIoperate.Addr = 0;
											UIoperate.RdFlag = 0;
											UIoperate.WrFlag = 0;
										  if(UIposition == 3)
											{
												LinkTypetempH = ConvertKey(valuekey.val);
											}
											else if(UIposition == 4)
											{
												LinkTypetempM = ConvertKey(valuekey.val);

											}else if(UIposition == 5)
											{
												LinkTypetempL = ConvertKey(valuekey.val);	
											}
											
											ReadLinkType = ( LinkTypetempH * 100) + ( LinkTypetempM * 10 ) + LinkTypetempL;
											UIposition++;
											if(UIposition == 6)UIposition = 3;
											
											disp_ui_v( ReadLinkType/100, ReadLinkType/10%10, ReadLinkType%10);	
											break;
											
											
									case 0x25:     								//WRITE
									
											BEEP_ON();
									
											UIoperate.WrFlag = 1;
											UIoperate.RdFlag = 0;									
											if(ReadLinkType > 0x2)
											{	
												UIoperate.Wrong = 1;
												UIoperate.Correct = 0;
												disp_ui_v( ReadLinkType/100, ReadLinkType/10%10, ReadLinkType%10);	

												break;
											}
											tmp.ch = 0;
											tmp.cmd = 0x4e;
											tmp.addr = 0;
											tmp.dat = ReadLinkType;
											write_cmfbuf(&tmp);
											UIoperate.Correct = 0;										
											disp_ui_v( ReadLinkType/100, ReadLinkType/10%10, ReadLinkType%10);		

									break;
											
											
									case 0x27:     
														break;

									case 0x37:      							//LIGHT
										/*										 
										 BEEP_ON();
										 if(UIoperate.Backlight == 0)
										 {
											 UIoperate.Backlight = 1;
											 LCDBLACKON(); 
										 }
										 else
										 {
											 UIoperate.Backlight = 0;
											 LCDBLACKOFF();											 
										 }*/
														break;

									case 0x47: 										//TEST
																	
														break;
									
								
									case 0x57: 					 					//SET
										
									  UIoperate.RdFlag = 0;
										UIoperate.WrFlag = 0;
									  UINUM++;
									  UINUM = UINUM % UINUM_MAX;
									 
														break;
									
									
									case 0x11: 					 					//ESC
														break;
									case 0x13: 					 					//CONTINUE

														break;
									case 0x15: 					 					//OK
														break;
									case 0x17: 					 					//CODE											
														break;				 
							 }			 											
	}
	
		if(strLcdD.UIinit)
		{
			strLcdD.UIinit = 0;
			UIoperate.Correct = 0;
			UIoperate.Wrong = 0;
			UIoperate.Addr = 0;
			UIoperate.Test = 0;
			UIoperate.Data = 0;
			UIoperate.CodeFlag = 1;
			UIoperate.LockFlag = 1;
      UIoperate.NowSet = 5;
			UIoperate.RdFlag = 0;
			UIoperate.WrFlag = 0;
			UIoperate.Backlight = 0;
			UIposition = 3;
			ReadLinkType = 0;
			LinkTypetempH = 0;
			LinkTypetempM = 0;
			LinkTypetempL = 0;
		}
		


		disp_ui_v( ReadLinkType/100, ReadLinkType/10%10, ReadLinkType%10);		
		strLcdD.UIfresh = 0;	
		
		

}
